import {ItemInterface} from "./itemInterface";
import {CreateDomWithList} from "./CreateDomWithList";
import {init} from "snabbdom/src/package/init";
import {styleModule} from "snabbdom/src/package/modules/style";
import {eventListenersModule} from "snabbdom/src/package/modules/eventlisteners";
import {ConfigureElementForVNode} from "./ConfigureElementForVNode";

let patch = init([
    styleModule, eventListenersModule
]);

let sortItems = (originalData:Array<ItemInterface>, sortName:string, type:string) => {
    console.log('排序操作');
    let ulList = document.querySelector('#ulList');
    let oldDOM = CreateDomWithList(originalData);
    ConfigureElementForVNode(oldDOM);
    if (type === "number")
        originalData = originalData.sort(
            (a, b) => a[sortName] - b[sortName]);

    else if (type === "string")
        originalData = originalData.sort(
            (a, b) => a[sortName].localeCompare(b[sortName]));

    let newDOM = CreateDomWithList(originalData);
    ConfigureElementForVNode(newDOM);
    patch(oldDOM, newDOM);
};

export {sortItems};